package com.game.model;

import com.game.global.Const;
import com.game.main.Executor;
import com.game.util.ImageUtil;
import com.game.util.PropertiesUtil;

import java.awt.*;
import java.util.Random;

/**
 * @Author: tjc
 * @description
 * @Date Created in 15:20 2021-08-01
 */
public class Enemy extends Tank {

    public Enemy() {
        SIZE_X = PropertiesUtil.loadInt("enemy.width");
        SIZE_Y = PropertiesUtil.loadInt("enemy.length");
        speed = PropertiesUtil.loadInt("enemy.speed");
        curDirection = Const.DIRECTION_UP;
    }

    public void start() {
        Executor.async(this::MoveTask, this::ShootTask);
    }

    public void MoveTask() {
        while (alive()) {
            curDirection = randomDirection();
            System.out.println("direction:" + curDirection);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void ShootTask() {
        while (alive()) {
            shoot();
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void move() {
        if (isBlocking()) {
            return;
        }
        switch (curDirection) {
            case Const.DIRECTION_UP:
                localY = localY - speed;
                break;
            case Const.DIRECTION_DOWN:
                localY = localY + speed;
                break;
            case Const.DIRECTION_LEFT:
                localX = localX - speed;
                break;
            case Const.DIRECTION_RIGHT:
                localX = localX + speed;
                break;
            default:
        }
    }

    private int randomDirection() {
        int i = new Random().nextInt(4);
        switch (i) {
            case 0:
                return Const.DIRECTION_UP;
            case 1:
                return Const.DIRECTION_DOWN;
            case 2:
                return Const.DIRECTION_LEFT;
            case 3:
                return Const.DIRECTION_RIGHT;
            default:
                return Const.DIRECTION_DOWN;
        }
    }

    @Override
    public void draw(Graphics g) {
        move();
        switch (this.curDirection) {
            case Const.DIRECTION_UP:
                g.drawImage(ImageUtil.enemyImages[0], localX, localY, SIZE_X, SIZE_Y, null);
                break;
            case Const.DIRECTION_DOWN:
                g.drawImage(ImageUtil.enemyImages[1], localX, localY, SIZE_X, SIZE_Y, null);
                break;
            case Const.DIRECTION_LEFT:
                g.drawImage(ImageUtil.enemyImages[2], localX, localY, SIZE_X, SIZE_Y, null);
                break;
            case Const.DIRECTION_RIGHT:
                g.drawImage(ImageUtil.enemyImages[3], localX, localY, SIZE_X, SIZE_Y, null);
                break;
            default:
                throw new RuntimeException("方向参数错误!");
        }
    }

    @Override
    public int getTankType() {
        return Tank.T_ENEMY;
    }
}
